package cn.itcast.controller;

import cn.itcast.dao.Company;
import cn.itcast.service.CompanyService;
import cn.itcast.service.impl.CompanyServiceImpl;
import cn.itcast.util.BeanUtil;
import com.github.pagehelper.PageInfo;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@WebServlet("/controller/company/*")
public class CompanyServlet extends BaseServlet {
    //查询所有
    public void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String page = req.getParameter("page");
        int pageNum = 1;
        if(page != null && !page.equals("")) {
            pageNum = Integer.parseInt(page);
        }


        CompanyService companyService = new CompanyServiceImpl();
        List<Company> list = companyService.findAll(pageNum);
        PageInfo pageInfo = new PageInfo(list);
        req.setAttribute("page",pageInfo);
        req.getRequestDispatcher("/pages/store/company/list.jsp").forward(req,resp);

    }
    //根据id查询company对象，回显页面
    public void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        CompanyService companyService = new CompanyServiceImpl();
        Company company = companyService.toEdit(id);
        req.setAttribute("company",company);
        req.getRequestDispatcher("/pages/store/company/update.jsp").forward(req,resp);

    }

    //修改数据保存至数据库同时在页面遍历显示
    public void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String, String[]> map = req.getParameterMap();
        Company company = new Company();
        try {
            BeanUtils.populate(company,map);
            CompanyService companyService = new CompanyServiceImpl();
            companyService.edit(company);
            req.getRequestDispatcher("/controller/company/findAll").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //添加企业
    public void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String, String[]> map = req.getParameterMap();
        Company company = new Company();
        try {
            BeanUtils.populate(company,map);
            CompanyService companyService = new CompanyServiceImpl();
            companyService.save(company);
            req.getRequestDispatcher("/controller/company/findAll").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //删除企业
    public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        CompanyService companyService = new CompanyServiceImpl();
        companyService.deleteById(id);
        req.getRequestDispatcher("/controller/company/findAll").forward(req,resp);
    }
}
